/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package state;

import java.util.Iterator;
import shapes.SnazzyShape;

/**
 * A renderable instance of a SpriteType
 * @author Andrew Gratta <andrew.gratta@stonybrook.edu>
 */
public class Sprite {
    private SpriteType spriteType;
    private float positionX;
    private float positionY;
    private Animation currentAnimation;
    private int animationCounter;
    private Pose currentPoseToEdit;
    private Pose currentPoseToRender;
    private Iterator<Pose> poseIterator;
    
    public Sprite(SpriteType type)
    {
        spriteType = type;
    }
//    /**
//     * Accessor method for the animation counter, which is used .
//     * 
//     * @return The animation counter for this sprite.
//     */
//    public int getAnimationCounter() { return animationCounter; }

    /**
     * Get the current animation
     * @return 
     */
    public Animation getCurrentAnimation() { return currentAnimation;}
    
    /**
     * This method retrieves and returns the Image that should
     * currently be used to render this Sprite.
     * 
     * @return A loaded Image representing the current state
     * of this Sprite.
     */
    public Pose getCurrentPoseToEdit()
    {
        return currentPoseToEdit;
    }
    
    public Pose getCurrentPoseToRender()
    {
        return currentPoseToRender;
    }
    
    public void clearPreview()
    {
        currentPoseToRender = null;
        poseIterator = null;
    }
    
    public void setPose(Pose pose)
    {
        currentPoseToEdit = pose;
    }
    
    /**
     * Accessor method for getting this Sprite's positionX-axis position.
     * 
     * @return The current positionX-axis position of this Sprite in the scene.
     */
    public float    getPositionX() { return positionX; }

    /**
     * Accessor method for getting this Sprite's positionY-axis position.
     * 
     * @return The current positionY-axis position of this Sprite in the scene.
     */
    public float    getPositionY() { return positionY; }
    
    
    // MUTATOR METHODS
    
    /**
     * Mutator method for setting this Sprite's positionX-axis position.
     * 
     * @param initPositionX The new positionX-axis position of this Sprite in the scene.
     */
    public void setPositionX(float initPositionX)
    {
        positionX = initPositionX;
    }

    /**
     * Mutator method for setting this Sprite's positionY-axis position.
     * 
     * @param initPositionY The new positionY-axis position of this Sprite in the scene.
     */
    public void setPositionY(float initPositionY)
    {
        positionY = initPositionY;
    }
    
    /**
     * This method updates the current pose to be used
     * for rendering each frame. Note that the speed of
     * animation can be scaled via the scaler argument.
     * 
     * @param timeScalar Scales the speed of animation, with 1.0f
     * leaving it as is, numbers greater than 1.0f decreasing
     * the speed, and numbers lower than 1.0f increasing the speed.
     */
    public void animate(float timeScalar)
    {
        if (poseIterator == null)
        {
            return;
        }
        // FIRST SCALE THE DURATION
        float duration = currentPoseToRender.getDuration();
        duration *= timeScalar;
        int roundedDuration = (int)(Math.round(duration));

        // AND WHEN TIME IS UP, FLIP THE FRAME
        if (animationCounter >= roundedDuration)
        {
            currentPoseToRender = poseIterator.next();
            animationCounter = 0;
        }
        else
        {
            animationCounter++;
        }
    }
    
    /**
     * Sets the current animation to the one that is associated to the given name.
     * If the animation does not exist, the animation will be set to null
     * @param name The name of the desired animation
     */
    public void setAnimation(String name)
    {
        currentAnimation = spriteType.getAnimation(name);
        if (currentAnimation != null)
        {
            poseIterator = currentAnimation.getCircularPoseIterator();
            currentPoseToRender = poseIterator.next();
        }
        else
        {
            currentPoseToEdit = null;
        }
    }
    
    /**
     * Get the shapes of the current pose
     * @return 
     */
    public Iterator<SnazzyShape> getShapes()
    {
        return currentPoseToEdit.getShapesIterator();
    }
    
    public Iterator<SnazzyShape> getAnimatorShapes()
    {
        if (currentPoseToRender == null)
        {
            return null;
        }
        return currentPoseToRender.getShapesIterator();
    }
    
    public void setSpriteType(SpriteType type)
    {
        spriteType = type;
        currentAnimation = null;
        currentPoseToEdit = null;
    }
    
    public SpriteType getSpriteType()
    {
        return spriteType;
    }
}
